package com.knight.boot.controller;

import com.knight.boot.pojo.User;
import com.knight.boot.servise.UserService;
import com.knight.boot.utils.ValidateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Guohai
 * @Date: 2020-07-28 11:30
 */
@Controller
@Slf4j
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/user/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 用户登录
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/user/loginUser")
    public String loginUser(User user, HttpServletRequest request) {
        log.info("start loginUser");
        User userFinded = userService.loginUser(user);
        System.err.println(userFinded);
        if (userFinded == null) {
            request.setAttribute("msg", "用户名或密码有误，请重新输入。");
            return "login";
        } else {
            request.getSession().setAttribute("user", userFinded);
            request.setAttribute("msg", "登录成功。");
            return "main";
        }
    }

    /**
     * 验证码实现
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/user/captcha", method = RequestMethod.GET)
    public void captcha(@RequestParam(value = "time") String time, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ValidateCode code= new ValidateCode(130,35,30,"validateCode");
        code.getCode(request,response);
    }

}
